using System;
using Microsoft.Win32;
using System.Configuration;
using System.Diagnostics;

namespace OR.Lazarus.Config
{
	/// <summary>
	/// Summary description for SettingsManager.
	/// </summary>
	public class SettingsManager
	{
        Configuration mConfig;

		public SettingsManager()
		{
            mConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        }

		public void LogErrorMissingKey( string p_szKey )
		{
			throw new ApplicationException( "The key " + p_szKey + " must be specified in the App.Config file." );
		}

		public void SetValue( string key, string value )
		{
            KeyValueConfigurationElement element = mConfig.AppSettings.Settings[key];

            if (element == null)
            {
                mConfig.AppSettings.Settings.Add(key, value);
            }
            else
            {
                element.Value = value;
            }
		}

		public string GetValue( string key )
		{
            KeyValueConfigurationElement element = mConfig.AppSettings.Settings[key];

            if (element == null)
            {
                return null;
            }
            else
            {
                return (element.Value);
            }
		}

        public void Save()
        {
            Debug.Assert(mConfig != null);

            mConfig.Save();
        }
	}
}
